//
//  MqInfoTopicTableViewController.m
//  Meiqi
//
//  Created by 王郑 on 16/1/16.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqInfoTopicTableViewController.h"
#import "MqInfoTopicTableViewCell.h"
#import "MqInfoTopicModel.h"


@interface MqInfoTopicTableViewController ()

// 数据源数组
@property (strong, nonatomic) NSMutableArray *listArray;
// 请求的起始位置
@property (assign, nonatomic) NSInteger requestStart;



@end

@implementation MqInfoTopicTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
#pragma mark   ---- tableView
    self.tableView.rowHeight = 220*kHeightBase;
    self.tableView.backgroundColor = [UIColor grayColor];
    [self.tableView registerClass:[MqInfoTopicTableViewCell class] forCellReuseIdentifier:@"topicCell"];
#pragma mark   ---- 数据源
    _listArray = [NSMutableArray array];
    _requestStart = 1;

#pragma mark   ---- 解析数据
    [self setUpData];
    
#pragma mark   ---- 上拉加载
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       
        _requestStart += 1;
        
        [self setUpData];
    }];
    
#pragma mark   ---- 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
        _requestStart = 1;
        
        // 清空数据源数组
        
        [self setUpData];
        
    }];

}


- (void)setUpData
{
    
    [PKRequestManager requestWithUrlString:[NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=render&&classid=1000&page=%ld&platform=1&pz=20&showType=specialList&time=0&version=1",(long)_requestStart] parDic:nil Method:GET finish:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        
        NSArray *list = dic[@"list"];
        
        if (_requestStart == 1) {
            
            [_listArray removeAllObjects];
        }
        
        for (NSDictionary *oneDic in list) {
            
            MqInfoTopicModel *model = [[MqInfoTopicModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_listArray addObject:model];
            
        }
        
#pragma mark ---- 结束加载和刷新
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
        
        
        
    } error:^(NSError *error) {
//        NSLog(@"%@",error);
        
    } path:@"topicData"];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MqInfoTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topicCell" forIndexPath:indexPath];
    
    // 展示数据
    MqInfoTopicModel *model = _listArray[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    // 创建通知让主页面去跳转
    MqInfoTopicModel *model = _listArray[indexPath.row];
    
    NSDictionary *dic = @{
                          @"id": model.ztid,
                          @"model":model
                          };
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"topicPush" object:self userInfo:dic];
    
    
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
